/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jholmedia.transindica;

import com.ibm.icu.text.Transliterator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kinjal
 */
public class Transform {

    private String outputFolder;
    private String inputFolder;
    private Transliterator t;

    public void setOutputFolder(String outputFolder) {
        this.outputFolder = outputFolder;
    }

    public void setInputFolder(String inputFolder) {
        this.inputFolder = inputFolder;
    }

    public void loadRuleSet(String ruleSet) {
        //ruleSet = "latin-interindic";
        t = Transliterator.getInstance(ruleSet);
        //System.out.println(t.toRules(true));
    }
    
    public void loadRuleFile(String ruleFile) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(ruleFile), "UTF-8"));
            StringBuffer rules = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.trim().length() == 0) {
                    continue;
                }
                if (line.charAt(0) == '\ufeff') {
                    continue;
                }
                if (line.charAt(0) == '#') {
                    continue;
                }
                rules.append(line);
                rules.append(System.getProperty("line.separator"));
            }
            reader.close();

            t = Transliterator.createFromRules("bn1-u0980", rules.toString(), Transliterator.FORWARD);
        } catch (IOException ex) {
            Logger.getLogger(Transform.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(Transform.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        /*
        Enumeration e = Transliterator.getAvailableIDs();
        while (e.hasMoreElements()) {
            String id = (String) e.nextElement();
            System.out.println(id+":"+Transliterator.getDisplayName(id));
        }
         */
    }

    public void transform(String sourceFile, String targetFile) {
        BufferedReader reader = null;
        try {
            String line = null;

            reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFolder + sourceFile), "UTF-8"));
            StringBuffer output = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                output.append(transform(line));
                output.append(System.getProperty("line.separator"));
            }
            reader.close();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFolder + targetFile), "UTF-8"));
            writer.write(output.toString());
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(Transform.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(Transform.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String transform(String sourceText) {
        return t.transliterate(sourceText);
    }
}
